/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-26 13:45:20
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:30:44
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import {
    BomContentServiceProxy, WorkFlowServiceProxy, AffairRemindServiceProxy,
    MoadifManageServiceProxy, BusinessApplicationModifInput, WorkAuditorDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'app-produre-approval',
    templateUrl: './produre-approval.component.html',
    styleUrls: ['./produre-approval.component.less']
})
export class ProdureApprovalComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    saving = false;
    active = false;
    actived = false;
    approval = undefined;// 选择审批流
    nodeFlag = false;
    saveFlag = false;
    approvalOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    approvalPersonOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    approvalForm = [];//审批流程图数据
    deatilForm = {
        label: undefined,
        choose: true,
        approvalRole: undefined,
        remark: undefined,
        roleName: undefined,
        approvalPerson: undefined,
        personName: undefined,
        wnID: undefined,
        wfID: undefined
    };
    approvalRoleOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    msgIndex: any;
    cutFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _workFlowServiceProxy: WorkFlowServiceProxy,
        private _affairRemindServiceProxy: AffairRemindServiceProxy,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    save() {
        let input = new BusinessApplicationModifInput();
        input.workAuditorList = [];
        for (const item of this.approvalForm) {
            if (item.approvalPerson == undefined) {
                this._messageService.info('请先维护审批人员');
                return;
            }
            let approval = new WorkAuditorDto();
            approval.wfID = item.wfID;
            approval.wnID = item.wnID;
            approval.userId = item.approvalPerson;
            input.workAuditorList.push(approval);
        }
        input.type = this.approval;
        input.modifyManage = this.editData;
        if (this.cutFlag) {
            this.cutFlag = false;
            this._moadifManageServiceProxy.pushMoadifManage(input)
                .pipe(finalize(() => { }))
                .subscribe((result) => {
                    this.cutFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.approvalForm = [];
                        this.modalSave.emit();
                        this.close();
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }
    }
    onShown(): void {
        this.approvalOption = [];
        this.approval = undefined;
        this._workFlowServiceProxy.getWorkDropList().pipe(finalize(() => { })).subscribe(result => {
            this.approvalOption = result;
            this.approval = result[0].value;
            this.searchMap(result[0].value);
        });
        this._affairRemindServiceProxy.getAffairRoles()
            .pipe(finalize(() => { }))
            .subscribe(rs => {
                this.approvalRoleOption = [
                    {
                        label: '请选择',
                        value: undefined
                    }
                ]
                for (const item of rs.items) {
                    let data = { label: '', value: undefined };
                    data.label = item.displayName;
                    data.value = item.id;
                    this.approvalRoleOption.push(data);
                }
            });
    }
    searchMap(id) {
        this.approvalForm = [];
        this._workFlowServiceProxy.getWorkNodeById(
            id
        )
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                let data = [];
                for (const item of result) {
                    let map = { label: '', approvalRole: undefined, remark: undefined, roleName: undefined, bappId: undefined, wnID: undefined, wfID: undefined };
                    map.label = item.wnName;
                    map.approvalRole = parseInt(item.handlers);
                    map.remark = item.nodeStatusDesc;
                    map.roleName = item.roleDesc;
                    map.wnID = item.wnID;
                    map.wfID = item.wfID;
                    data.push(map);
                }
                this.approvalForm = data;
            });
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        if (this.nodeFlag) {
            this._messageService.warn('请先维护完当前节点信息!');
            return;
        }
        this.modal.hide();
    }
    onChange(event) {
        this.searchMap(event);
        this.nodeFlag = false;
    }
    showDetail(row, index) {
        setTimeout(() => {
            //节点详细信息
            this.deatilForm = { ...row };
            this.deatilForm.choose = true;
            this.msgIndex = index;
            this.nodeFlag = true;
            this.approvalPersonOption = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            //根据角色ID查人
            this._workFlowServiceProxy.getUserByRole(this.deatilForm.approvalRole).pipe(finalize(() => { })).subscribe(result => {
                for (const item of result) {
                    let rs = { label: undefined, value: undefined };
                    rs.label = item.name;
                    rs.value = item.id;
                    this.approvalPersonOption.push(rs);
                }
                this.actived = true;
            });
        }, 10);
    }
    onHide(event) {
        this.nodeFlag = false;
        this.actived = false;
        this.deatilForm.approvalRole = undefined;
        this.deatilForm.choose = true;
        this.deatilForm.label = undefined;
        this.deatilForm.remark = undefined;
        this.deatilForm.approvalPerson = undefined;
    }
    saveNode() {
        this.saveFlag = true;
        let that = this;
        for (const item of that.approvalPersonOption) {
            if (item.value == that.deatilForm.approvalPerson) {
                that.deatilForm.personName = item.label;
            }
        }
        this.approvalForm[this.msgIndex] = Object.assign({}, that.deatilForm);
        setTimeout(() => {
            this.nodeFlag = false;
        }, 10);
    }
}
